package com.jswlw.service.base.impl;

import com.jswlw.dao.base.DictionaryDataMapper;
import com.jswlw.dao.base.DictionaryMapper;
import com.jswlw.entity.base.Dictionary;
import com.jswlw.entity.base.DictionaryData;
import com.jswlw.service.base.DictionaryDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wu on 2017-12-14.
 */
@Service
public class DictionaryDataServiceImpl implements DictionaryDataService {

    @Autowired
    DictionaryDataMapper dictionaryDataMapper;

    @Autowired
    DictionaryMapper dictionaryMapper;

    /**
     * 数据字典数据
     */
    private Map<String, List<DictionaryData>> dictDatas;

    private List<DictionaryData>  list;

    @Override
    public List<DictionaryData> findAll(Map condition) {
        return dictionaryDataMapper.findAll(condition);
    }

    @Override
    public DictionaryData findOne(Integer id) {
        return dictionaryDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insert(DictionaryData dictionaryData) {
        return dictionaryDataMapper.insertSelective(dictionaryData);
    }

    @Override
    public int update(DictionaryData dictionaryData) {
        return dictionaryDataMapper.updateByPrimaryKeySelective(dictionaryData);
    }

    @Override
    public int delete(Integer id) {
        return dictionaryDataMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int findCount(Map map) {
        return dictionaryDataMapper.findCount(map);
    }

    @Override
    public List<DictionaryData> searchPage(Map map) {
        return null;
    }

    @Override
    public List<DictionaryData> findDictData4DictCode(String code) {
        //缓存数据到内存
        if (dictDatas == null) {
            dictDatas = new HashMap<>();
            //数据为空查询数据缓存
            List<Dictionary> dictionaries = dictionaryMapper.findAll(new HashMap());
            //循环查询数据放入缓存
            for (int i = 0; i < dictionaries.size(); i++) {
                Dictionary dictionary = dictionaries.get(i);
                Map condition = new HashMap();
                condition.put("dict", dictionary.getId());
                List<DictionaryData> dictionaryDatas = dictionaryDataMapper.findAll(condition);
                dictDatas.put(dictionary.getCode(), dictionaryDatas);
            }
        }
        if (dictDatas.get(code) == null || dictDatas.get(code).size() == 0) {
            Map condition = new HashMap();
            condition.put("code", code);
            List<Dictionary> dictionaries = dictionaryMapper.findAll(condition);
            Map conditionData = new HashMap();
            if (dictionaries.size() > 0) {
                conditionData.put("dict", dictionaries.get(0).getId());
                dictDatas.put(code, dictionaryDataMapper.findAll(conditionData));
            }
        }
        return dictDatas.get(code);
    }

    @Override
    public DictionaryData findOne4Cache(Integer id) {
        if (list == null) {
            list=dictionaryDataMapper.findAll(new HashMap());
        }
        for(int i=0;i<list.size();i++){
            DictionaryData dictionaryData=list.get(i);
            if(dictionaryData.getId().equals(id)){
                return dictionaryData;
            }
        }
        return null;
    }
}
